/**
 *    Copyright (c) [2021] [白晓明]
 *    [IMHarmonyOSLib] is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */
package com.itming.ui.coptlib.core;

import com.itming.ui.coptlib.enums.IMButtonSize;
import com.itming.ui.coptlib.enums.IMButtonType;
import com.itming.ui.coptlib.util.ColorUtils;
import com.itming.ui.coptlib.util.Constant;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Button;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class IMButton extends Button {

    private static final String TAG = "IMButton";
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.DEBUG, 0, TAG);

    public IMButton(Context context) {
        this(context, null);
    }

    public IMButton(final Context context, final AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public IMButton(final Context context, final AttrSet attrSet, final String styleName) {
        super(context, attrSet, styleName);
        initIMButton(attrSet);
    }

    private void initIMButton(AttrSet attrSet) {
        //是否镂空按钮
        if (attrSet.getAttr("plain").isPresent()) {
            setPlain(attrSet.getAttr("plain").get().getBoolValue());
        } else {
            setPlain(false);
        }

        //设置圆角
        if (attrSet.getAttr("round").isPresent()) {
            setRound(attrSet.getAttr("round").get().getBoolValue());
        } else {
            setRound(false);
        }

        //设置圆形按钮
        if (attrSet.getAttr("circle").isPresent()) {
            setCircle(attrSet.getAttr("circle").get().getBoolValue());
        } else {
            setCircle(false);
        }

        //是否禁用状态
        if (attrSet.getAttr("disabled").isPresent()) {
            setDisabled(attrSet.getAttr("disabled").get().getBoolValue());
        } else {
            setDisabled(false);
        }

        //设置按钮内容
        if (attrSet.getAttr("content").isPresent()) {
            setText(attrSet.getAttr("content").get().getStringValue());
        } else {
            setText("");
        }

        //根据类型设置背景色
        if (attrSet.getAttr("type").isPresent()) {
            setType(IMButtonType.valueOf(attrSet.getAttr("type").get().getStringValue()));
        } else {
            setType(IMButtonType.NORMAL);
        }
        setIMButtonBackground();

        //根据尺寸设置按钮大小
        if (attrSet.getAttr("size").isPresent()) {
            setSize(IMButtonSize.valueOf(attrSet.getAttr("size").get().getStringValue()));
        } else {
            setSize(IMButtonSize.MEDIUM);
        }
        setIMButtonSize();
    }


    /**
     * 设置按钮尺寸
     */
    private void setIMButtonSize() {
        switch (getSize()) {
            case SMALL:
                setTextSize(16, TextSizeType.FP);
                setPadding(70, 35, 70, 35);
                break;
            case MINI:
                setTextSize(14, TextSizeType.FP);
                setPadding(50, 25, 50, 25);
                break;
            default:
                setTextSize(18, TextSizeType.FP);
                setPadding(100, 50, 100, 50);
                break;
        }
    }

    /**
     * 设置按钮背景
     */
    private void setIMButtonBackground() {
        setTextColor(Color.WHITE);
        switch (getType()) {
            case PRIMARY:
                if (isPlain()) {
                    defaultBtnBgColor = ColorUtils.BTN_PRIMARY_PLAIN;
                } else {
                    defaultBtnBgColor = ColorUtils.BTN_PRIMARY;
                }
                defaultBtnBorder = ColorUtils.BTN_PRIMARY;
                break;
            case SUCCESS:
                if (isPlain()) {
                    defaultBtnBgColor = ColorUtils.BTN_SUCCESS_PLAIN;
                } else {
                    defaultBtnBgColor = ColorUtils.BTN_SUCCESS;
                }
                defaultBtnBorder = ColorUtils.BTN_SUCCESS;
                break;
            case WARNING:
                if (isPlain()) {
                    defaultBtnBgColor = ColorUtils.BTN_WARNING_PLAIN;
                } else {
                    defaultBtnBgColor = ColorUtils.BTN_WARNING;
                }
                defaultBtnBorder = ColorUtils.BTN_WARNING;
                break;
            case DANGER:
                if (isPlain()) {
                    defaultBtnBgColor = ColorUtils.BTN_DANGER_PLAIN;
                } else {
                    defaultBtnBgColor = ColorUtils.BTN_DANGER;
                }
                defaultBtnBorder = ColorUtils.BTN_DANGER;
                break;
            case INFO:
                if (isPlain()) {
                    defaultBtnBgColor = ColorUtils.BTN_INFO_PLAIN;
                } else {
                    defaultBtnBgColor = ColorUtils.BTN_INFO;
                }
                defaultBtnBorder = ColorUtils.BTN_INFO;
                break;
            default:
                setTextColor(Color.BLACK);
                defaultBtnBgColor = ColorUtils.BTN_NORMAL;
                defaultBtnBorder = ColorUtils.BTN_NORMAL_BORDER;
                break;
        }
        if (getType().equals(IMButtonType.TEXT)) {
            setTextColor(new Color(ColorUtils.BTN_TEXT_COLOR));
        } else {
            ShapeElement element = new ShapeElement();
            if (isPlain()) {
                setTextColor(Color.BLACK);
            }
            if (isRound()) {
                defaultRound = Constant.DEFAULT_SETTING_ROUND;
            }
            if (isCircle()) {
                element.setShape(ShapeElement.OVAL);
            } else {
                element.setCornerRadius(defaultRound);
            }
            if (isDisabled()) {
                setAlpha(0.4f);
                setClickable(false);
            }
            element.setRgbColor(RgbColor.fromRgbaInt(defaultBtnBgColor));
            element.setStroke(Constant.DEFAULT_BORDER_WIDTH, RgbColor.fromRgbaInt(defaultBtnBorder));
            setBackground(element);
        }
    }


    private int defaultBtnBgColor = ColorUtils.BTN_NORMAL;
    private int defaultBtnBorder = ColorUtils.BTN_NORMAL_BORDER;
    private float defaultRound = Constant.DEFAULT_ROUND;
    /**
     * 尺寸（medium/small/mini）
     */
    private IMButtonSize size;
    /**
     * 类型（primary/success/warning/danger/info/text）
     */
    private IMButtonType type;
    /**
     * 是否镂空（默认false）
     */
    private boolean plain = false;
    /**
     * 是否圆角按钮（默认false）
     */
    private boolean round = false;

    /**
     * 是否圆形按钮（默认false）
     */
    private boolean circle = false;

    /**
     * 是否禁用状态
     */
    private boolean disabled = false;

    public IMButtonSize getSize() {
        return this.size;
    }

    public void setSize(final IMButtonSize size) {
        this.size = size;
    }

    public IMButtonType getType() {
        return this.type;
    }

    public void setType(final IMButtonType type) {
        this.type = type;
    }

    public boolean isPlain() {
        return plain;
    }

    public void setPlain(boolean plain) {
        this.plain = plain;
    }

    public boolean isRound() {
        return this.round;
    }

    public void setRound(final boolean round) {
        this.round = round;
    }

    public boolean isCircle() {
        return this.circle;
    }

    public void setCircle(final boolean circle) {
        this.circle = circle;
    }

    public boolean isDisabled() {
        return this.disabled;
    }

    public void setDisabled(final boolean disabled) {
        this.disabled = disabled;
    }
}
